package com.bing.bingPro;

import java.lang.reflect.Field;

public class ReflectUtil {

    public static void copyAndReplaceNullsWithDashes(Object src, Object dest) throws IllegalAccessException {
        for (Field field : src.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object fieldValue = field.get(src);
            String stringRepresentation = fieldValue == null ? null : String.valueOf(fieldValue);

            try {
                Field destField = dest.getClass().getDeclaredField(field.getName());
                destField.setAccessible(true);

                if (destField.getType() == String.class) {
                    destField.set(dest, stringRepresentation);
                }
            } catch (NoSuchFieldException e) {
                // ignore field if it doesn't exist in destination object
            }
        }
    }


    public static void copyAndReplace(Object src, Object dest) throws IllegalAccessException {
        for (Field field : src.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object fieldValue = field.get(src);
            String stringRepresentation = fieldValue == null ? "-" : String.valueOf(fieldValue);

            try {
                Field destField = dest.getClass().getDeclaredField(field.getName());
                destField.setAccessible(true);

                if (destField.getType() == String.class) {
                    destField.set(dest, stringRepresentation);
                }
            } catch (NoSuchFieldException e) {
                // ignore field if it doesn't exist in destination object
            }
        }
    }
}
